1 using System;
2 using UnityEngine;
3 using Random = UnityEngine.Random;
4
5 [RequireComponent(typeof(Motor))]
6 public class CarAI : MonoBehaviour
7 {
8
9 private timer tim;
10
11 private bool enab = true;
12
13 public enum BrakeCondition
14 {
15 NeverBrake, // the car simply accelerates at full throttle all the time.
16 TargetDirectionDifference, // the car will brake according to the upcoming change in direction of the target. Useful for route-based AI, slowing for corners.
17 TargetDistance, // the car will brake as it approaches its target, regardless of the target's direction. Useful if you want the car to
18 // head for a stationary target and come to rest when it arrives there.
19 }
20
21 // This script provides input to the car controller in the same way that the user control script does.
22 // As such, it is really 'driving' the car, with no special physics or animation tricks to make the car behave properly.
23
24 // "wandering" is used to give the cars a more human, less robotic feel. They can waver slightly
25 // in speed and direction while driving towards their target.
26
27 [SerializeField]
28 [Range(0, 1)]
29 private float m_CautiousSpeedFactor = 0.05f; // percentage of max speed to use when being maximally cautious
30 [SerializeField]
31 [Range(0, 180)]
32 private float m_CautiousMaxAngle = 50f; // angle of approaching corner to treat as warranting maximum caution
33 [SerializeField]
34 private float m_CautiousMaxDistance = 100f; // distance at which distance-based cautiousness begins
35 [SerializeField]
36 private float m_CautiousAngularVelocityFactor = 30f; // how cautious the AI should be when considering its own current angular velocity (i.e. easing off acceleration if spinning!)
37 [SerializeField]
38 private float m_SteerSensitivity = 0.05f; // how sensitively the AI uses steering input to turn to the desired direction
39 [SerializeField]
40 private float m_AccelSensitivity = 0.04f; // How sensitively the AI uses the accelerator to reach the current desired speed
41 [SerializeField]
42 private float m_BrakeSensitivity = 1f; // How sensitively the AI uses the brake to reach the current desired speed
43 [SerializeField]
44 private float m_LateralWanderDistance = 3f; // how far the car will wander laterally towards its target
45 [SerializeField]
46 private float m_LateralWanderSpeed = 0.1f; // how fast the lateral wandering will fluctuate
47 [SerializeField]
48 [Range(0, 1)]
49 private float m_AccelWanderAmount = 0.1f; // how much the cars acceleration will wander
50 [SerializeField]
51 private float m_AccelWanderSpeed = 0.1f; // how fast the cars acceleration wandering will fluctuate
52 [SerializeField]
53 private BrakeCondition m_BrakeCondition = BrakeCondition.TargetDistance; // what should the AI consider when accelerating/braking?
54 [SerializeField]
55 private bool m_Driving; // whether the AI is currently actively driving or stopped.
56 [SerializeField]
57 private Transform m_Target; // 'target' the target object to aim for.
58 [SerializeField]
59 private bool m_StopWhenTargetReached; // should we stop driving when we reach the target?
60 [SerializeField]
61 private float m_ReachTargetThreshold = 2; // proximity to target to consider we 'reached' it, and stop driving.
62
63 private float m_RandomPerlin; // A random value for the car to base its wander on (so that AI cars don't all wander in the same pattern)
64 public Motor m_CarController; // Reference to actual car controller we are controlling
65 private float m_AvoidOtherCarTime; // time until which to avoid the car we recently collided with
66 private float m_AvoidOtherCarSlowdown; // how much to slow down due to colliding with another car, whilst avoiding
67 private float m_AvoidPathOffset; // direction (-1 or 1) in which to offset path to avoid other car, whilst avoiding
68 private Rigidbody m_Rigidbody;
69
70 public float desiredSpeed;
71
72
73 private void Awake()
74 {
75 // get the car controller reference
76 m_CarController = GetComponent<Motor>();
77
78 // give the random perlin a random value
79 m_RandomPerlin = Random.value * 100;
80
81 m_Rigidbody = GetComponent<Rigidbody>();
82 tim = transform.root.GetComponentInChildren<timer>();
83 }
84
85
86 private void FixedUpdate()
87 {
88 if (m_Target == null || !m_Driving || !enab || !tim.startrace)
89 {
90 // Car should not be moving,
91 // use handbrake to stop
92 m_CarController.Move(0, 0, 1f, 1f);
93 }
94 else
95 {
96 Vector3 fwd = transform.forward;
97 if (m_Rigidbody.velocity.magnitude > m_CarController.MaxSpeed * 0.1f)
98 {
99 fwd = m_Rigidbody.velocity;
100 }
101
102 desiredSpeed = m_CarController.MaxSpeed;
103
104 // now it's time to decide if we should be slowing down...
105 switch (m_BrakeCondition)
106 {
107 case BrakeCondition.TargetDirectionDifference:
108 {
109 // the car will brake according to the upcoming change in direction of the target. Useful for route-based AI, slowing for corners.
110
111 // check out the angle of our target compared to the current direction of the car
112 float approachingCornerAngle = Vector3.Angle(m_Target.forward, fwd);
113
114 // also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
115 float spinningAngle = m_Rigidbody.angularVelocity.magnitude * m_CautiousAngularVelocityFactor;
116
117 // if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
118 float cautiousnessRequired = Mathf.InverseLerp(0, m_CautiousMaxAngle,
119 Mathf.Max(spinningAngle,
120 approachingCornerAngle));
121 desiredSpeed = Mathf.Lerp(m_CarController.MaxSpeed, m_CarController.MaxSpeed * m_CautiousSpeedFactor,
122 cautiousnessRequired);
123 break;
124 }
125
126 case BrakeCondition.TargetDistance:
127 {
128 // the car will brake as it approaches its target, regardless of the target's direction. Useful if you want the car to
129 // head for a stationary target and come to rest when it arrives there.
130
131 // check out the distance to target
132 Vector3 delta = m_Target.position - transform.position;
133 float distanceCautiousFactor = Mathf.InverseLerp(m_CautiousMaxDistance, 0, delta.magnitude);
134
135 // also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
136 float spinningAngle = m_Rigidbody.angularVelocity.magnitude * m_CautiousAngularVelocityFactor;
137
138 // if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
139 float cautiousnessRequired = Mathf.Max(
140 Mathf.InverseLerp(0, m_CautiousMaxAngle, spinningAngle), distanceCautiousFactor);
141 desiredSpeed = Mathf.Lerp(m_CarController.MaxSpeed, m_CarController.MaxSpeed * m_CautiousSpeedFactor,
142 cautiousnessRequired);
143 break;
144 }
145
146 case BrakeCondition.NeverBrake:
147 break;
148 }
149
150 // Evasive action due to collision with other cars:
151
152 // our target position starts off as the 'real' target position
153 Vector3 offsetTargetPos = m_Target.position;
154
155 // if are we currently taking evasive action to prevent being stuck against another car:
156 if (Time.time < m_AvoidOtherCarTime)
157 {
158 // slow down if necessary (if we were behind the other car when collision occured)
159 desiredSpeed *= m_AvoidOtherCarSlowdown;
160
161 // and veer towards the side of our path-to-target that is away from the other car
162 offsetTargetPos += m_Target.right * m_AvoidPathOffset;
163 }
164 else
165 {
166 // no need for evasive action, we can just wander across the path-to-target in a random way,
167 // which can help prevent AI from seeming too uniform and robotic in their driving
168 offsetTargetPos += m_Target.right *
169 (Mathf.PerlinNoise(Time.time * m_LateralWanderSpeed, m_RandomPerlin) * 2 - 1) *
170 m_LateralWanderDistance;
171 }
172
173 // use different sensitivity depending on whether accelerating or braking:
174 float accelBrakeSensitivity = (desiredSpeed < m_CarController.CurrentSpeed)
175 ? m_BrakeSensitivity
176 : m_AccelSensitivity;
177
178 // decide the actual amount of accel/brake input to achieve desired speed.
179 float accel = Mathf.Clamp((desiredSpeed - m_CarController.CurrentSpeed) * accelBrakeSensitivity, -1, 1);
180
181 // add acceleration 'wander', which also prevents AI from seeming too uniform and robotic in their driving
182 // i.e. increasing the accel wander amount can introduce jostling and bumps between AI cars in a race
183 accel *= (1 - m_AccelWanderAmount) +
184 (Mathf.PerlinNoise(Time.time * m_AccelWanderSpeed, m_RandomPerlin) * m_AccelWanderAmount);
185
186 // calculate the local-relative position of the target, to steer towards
187 Vector3 localTarget = transform.InverseTransformPoint(offsetTargetPos);
188
189 // work out the local angle towards the target
190 float targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
191
192 // get the amount of steering needed to aim the car towards the target
193 float steer = Mathf.Clamp(targetAngle * m_SteerSensitivity, -1, 1) * Mathf.Sign(m_CarController.CurrentSpeed);
194
195 // feed input to the car controller.
196 m_CarController.Move(steer, accel, 0f, 0f);
197
198 // if appropriate, stop driving when we're close enough to the target.
199 if (m_StopWhenTargetReached && localTarget.magnitude < m_ReachTargetThreshold)
200 {
201 m_Driving = false;
202 }
203 }
204 }
205
206
207 private void OnCollisionStay(Collision col)
208 {
209 // detect collision against other cars, so that we can take evasive action
210 if (col.rigidbody != null)
211 {
212 var otherAI = col.rigidbody.GetComponent<Motor>();
213 if (otherAI != null)
214 {
215 // we'll take evasive action for 1 second
216 m_AvoidOtherCarTime = Time.time + 1;
217
218 // but who's in front?...
219 if (Vector3.Angle(transform.forward, otherAI.transform.position - transform.position) < 90)
220 {
221 // the other ai is in front, so it is only good manners that we ought to brake...
222 m_AvoidOtherCarSlowdown = 0.5f;
223 }
224 else
225 {
226 // we're in front! ain't slowing down for anybody...
227 m_AvoidOtherCarSlowdown = 1;
228 }
229
230 // both cars should take evasive action by driving along an offset from the path centre,
231 // away from the other car
232 var otherCarLocalDelta = transform.InverseTransformPoint(otherAI.transform.position);
233 float otherCarAngle = Mathf.Atan2(otherCarLocalDelta.x, otherCarLocalDelta.z);
234 m_AvoidPathOffset = m_LateralWanderDistance * -Mathf.Sign(otherCarAngle);
235 }
236 }
237 }
238
239
240 public void SetTarget(Transform target)
241 {
242 m_Target = target;
243 m_Driving = true;
244 }
245
246 public void finished()
247 {
248 enab = false;
249 }
250
251 }